const fs = require('fs');
const path = require('path');
const readline = require("readline");
const { titleCase } = require("./generate")

function run() {
    let entityClassName = "";
    // 创建readline接口实例
    let r1 = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    })
    //close事件监听
    r1.on("close", function () {
        // 结束程序
        process.exit(0);
    })
    //调用接口方法
    r1.question("请输入类名\t", function (answer) {
        var re = new RegExp("^[a-zA-Z]+$");
        if (re.test(answer)) {
            entityClassName = titleCase(answer);
            generate(entityClassName);
        } else {
            console.log("输入类目错误，请重新输入");
        }
        r1.close();
    })
}


async function generate(entityClassName) {
    let lowEntityClassName = entityClassName.toLowerCase();

    let filePath = path.join(__dirname, './template/template.api.template');
    let res = fs.readFileSync(filePath).toString()
    res = res.replaceAll("${template}", entityClassName)
    res = res.replaceAll("template", lowEntityClassName)

    console.log('res', res)

    fs.writeFileSync(path.join(__dirname, `../${entityClassName}/${lowEntityClassName}.api.ts`), res)

}

run()